package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.TareaDAORemote;
import proyecto.EntityBeans.Tarea;

@Stateless
@Remote(TareaBLRemote.class)
public class TareaBL implements TareaBLRemote {

	TareaDAORemote tareaDAORemote;
	
	public int save(Tarea tarea) {	
		
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return tareaDAORemote.save(tarea); //alta de tarea		
		
	}
		
	public Tarea findById(int id) {
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return tareaDAORemote.findById(id);
		
	}
	
		
	public void merge(Tarea tarea) {
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
			tareaDAORemote.merge(tarea);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findByName(String name)
	{	
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return tareaDAORemote.findByName(name);
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return tareaDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return tareaDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idTarea){	
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		tareaDAORemote.remove(idTarea);
	}
	
	public void refresh(Tarea tarea){
		try
		{
			Context context = new InitialContext();
			tareaDAORemote = (TareaDAORemote) context.lookup("TareaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		tareaDAORemote.refresh(tarea);
	}
}
